import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import ElementUI from '../components/ElementUI.vue'
import Home from '../components/Home.vue'
import User from '../components/user/user.vue'
import ItemCat from'../components/items/ItemCat.vue'
import Item from'../components/items/Item.vue'
import AddItem from'../components/items/addItem.vue'

//使用路由机制
Vue.use(VueRouter)
const routes = [
  {path: '/', redirect: '/login'},
  {path: '/login', component: Login},
  {path: '/elementUI', component: ElementUI},
  {path: '/home',component: Home,
    children:[
      {path: '/user',component:User},
      {path: '/itemCat',component:ItemCat},
      {path:'/item',component:Item},
      {path:'/item/addItem',component:AddItem},
      ]
  },
  ]

//路由导航守卫!!!!!!!

const router = new VueRouter({
  routes
})
/**
 * 定义路由导航守卫
 * 参数1:   to       路由跳转的网址
 * 参数2:   from   路由从哪里来
 * 参数3:   next   是一个函数,表示放行或重定向
 *                       next()放行
 *                       next("/login")  重定向
 * 业务实现:
 *        核心逻辑:检查是否有token,如果有token,表示已经登陆,放行请求
 *                                             如果没有token,表示没有登陆,重定向到登陆页面
 *                      如果访问login页面,直接放行
 */
router.beforeEach((to,from,next)=>{
        if(to.path === "/login"){
          return next()
        }
        // 说明用户访问的页面不是login   请求需要检验
        let token = window.sessionStorage.getItem("token")
        if(token){
          return next()
        }else{
          alert("请登录!!!")
          return next("/login")
        }

})




export default router
